package com.atguigu.web;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/3/22 11:09
 */
public class StudentCheck {
    
    
    public static void main(String[] args) throws Exception {
        System.out.println("开始读取 班级所有学生...");
        List<String> allStudents = readAllStudents(args[0]);
        System.out.println("结束读取 班级所有学生...");
        //List<String> signInStudents = readSignInStudents(allStudents);
        //List<String> noSignInStudents = readNoSignInStudents(allStudents);
        List<String>[] singAndNoSign = readSignAndNoSignInStudents(allStudents);
        
        
        printNotInStudent(allStudents, singAndNoSign[0], singAndNoSign[1]);
    }
    
    
    private static List<String>[] readSignAndNoSignInStudents(List<String> allStudents) throws IOException {
        System.out.println("请输入所有签到和没有签到的学生名单(end表示输入结束): ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String all = "";
        String line = reader.readLine();
        
        try {
            while (line != null) {
                all += line;
                if (line.contains("end")) {
                    break;
                }
                
                
                line = reader.readLine();
            }
        } catch (Exception e) {
        }
        
        all = all.contains("未签到名单") ? all : (all + "未签到名单");
        String sign = all.replaceAll(".*已签到名单(.+)未签到名单.*", "$1");
        String noSign = all.replaceAll(".*未签到名单(.*)", "$1");
        
        List<String> a = getStudentList(Collections.singletonList(sign), allStudents);
        List<String> b = getStudentList(Collections.singletonList(noSign), allStudents);
        
        
        return new List[]{a, b};
    }
    
    private static void printNotInStudent(List<String> allStudents,
                                          List<String> signInStudents,
                                          List<String> noSignInStudents) {
        // allStudents - signInStudents - noSignInStudents = 没有进直播间的
        allStudents.removeAll(signInStudents);
        allStudents.removeAll(noSignInStudents);
        
        System.out.println("\n\n以下同学不在(" + allStudents.size() + "):");
        int i = 0;
        for (String noInStudent : allStudents) {
            System.out.print(noInStudent + "\t");
            if (++i % 10 == 0) {
                System.out.println();
            }
        }
        System.out.println("\n\n以下同学没有签到(" + noSignInStudents.size() + "):");
        i = 0;
        for (String noSignInStudent : noSignInStudents) {
            System.out.print(noSignInStudent + "\t");
            if (++i % 10 == 0) {
                System.out.println();
            }
        }
        System.out.println();
        
    }
    
    private static List<String> readNoSignInStudents(List<String> allStudents) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入没有签到的学生名单: ");
        
        return getStudentList(readStudents(reader), allStudents);
        
        
    }
    
    private static List<String> getStudentList(List<String> readStudents, List<String> allStudents) {
        
        ArrayList<String> result = new ArrayList<>();
        
        String students = "";
        for (String readStudent : readStudents) {
            students += readStudent;
        }
        for (String s : allStudents) {
            if (students.contains(s)) {
                result.add(s);
            }
        }
        return result;
    }
    
    private static List<String> readSignInStudents(List<String> allStudents) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入已经签到的学生名单: ");
        return getStudentList(readStudents(reader), allStudents);
    }
    
    private static List<String> readAllStudents(String path) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        
        return readStudents(reader);
        
    }
    
    private static List<String> readStudents(BufferedReader reader) throws IOException {
        ArrayList<String> studentList = new ArrayList<>();
        
        
        String line = reader.readLine();
        
        while (line != null && !line.contains("end")) {
            String[] students = line.split("\\s+");
            
            studentList.addAll(Arrays.asList(students));
            line = reader.readLine();
        }
        return studentList;
    }
    
    
}
